package com.oxygen.pt.service.impl;

import cn.hutool.core.lang.Pair;
import com.oxygen.pt.mapper.HyperbaricChamberMapper;
import com.oxygen.pt.pojo.vo.HyperbaricChamberUseStateVO;
import com.oxygen.pt.pojo.vo.Result;
import com.oxygen.pt.service.HyperbaricChamberService;
import com.oxygen.pt.utils.RedisConstants;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class HyperbaricChamberServiceImpl implements HyperbaricChamberService {

    @Resource
    private HyperbaricChamberMapper hyperbaricChamberMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 查询氧舱的使用情况，
     * @return 返回氧舱的 使用多少，总的容量多少，氧舱编号多少
     */
    @Override
    public Result<Object> getStates() {
        //查询氧舱的数据库信息
        List<Pair<Integer, Integer>> pairs = hyperbaricChamberMapper.selectChamberInfo();
        //查询氧舱的redis信息
        //stringRedisTemplate.opsForHash().get(RedisConstants.CHAMBER_INFORMATION);

        List<HyperbaricChamberUseStateVO> res = new ArrayList<>();
        for (Pair<Integer, Integer> pair : pairs) {
            //计算余量
            String s = (String) stringRedisTemplate.opsForHash().get(RedisConstants.CHAMBER_INFORMATION,
                    String.valueOf(pair.getKey()));
            int usageNum = pair.getValue() - Integer.parseInt(s);
            HyperbaricChamberUseStateVO chamberState
                    = new HyperbaricChamberUseStateVO(usageNum, pair.getValue(), pair.getKey());
            res.add(chamberState);
        }
        //拼装VO返回
        return Result.success(res);
    }
}
